package com.zhongkun.datahome.service.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongkun.datahome.service.domain.DecorationStandard;
import com.zhongkun.datahome.service.mapper.DecorationStandardMapper;
import com.zhongkun.datahome.service.req.qry.standard.DecorationStandardQry;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/17 13:36
 * @Desc
 */
@Repository
public class DecorationStandardRepository extends ServiceImpl<DecorationStandardMapper, DecorationStandard> {
    @SuppressWarnings("unchecked")
    public IPage<DecorationStandard> page(DecorationStandardQry qry) {
        IPage<DecorationStandard> page = new Page<>(qry.getPageNo(), qry.getPageSize());
        return this.page(page, new LambdaQueryWrapper<DecorationStandard>()
                .like(StringUtils.isNotBlank(qry.getStandardName()), DecorationStandard::getStandardName, qry.getStandardName())
                .orderByDesc(DecorationStandard::getUpdateTime, DecorationStandard::getId));

    }

    public Long countByName(String standardName) {
        return this.count(new LambdaQueryWrapper<DecorationStandard>()
                .eq(DecorationStandard::getStandardName, standardName));
    }

    public Set<String> getStandardNames(List<Long> standardIds) {
        if (CollectionUtils.isEmpty(standardIds)) {
            return Collections.emptySet();
        }

        return this.list(new LambdaQueryWrapper<DecorationStandard>()
                .select(DecorationStandard::getStandardName)
                .in(DecorationStandard::getId, standardIds))
                .stream()
                .map(DecorationStandard::getStandardName)
                .collect(Collectors.toSet());
    }
}
